$(document).ready(function(){
	var $historyGo = $('.historyGo');	//记录是否需要跳回前一个页面
	//如果存在cookie
	if(BC.CookieUtil.get('uid')!=null && BC.CookieUtil.get('password')!=null){
		var $loginContainer = $('.login-container'),
			oldContent = $loginContainer.html(),
			tipsPic = $('.modal .tips-pic')[0],
			$tips = $('.modal .tips-content'),
			$dialogBox = $('#dialog-box');
		$loginContainer.html('<p style="text-align:center">正在登陆...</p>');
		BC.ajax({
			type:'POST',
			url:'/bookcrossing/login',
			dataType:'json',
			data:{username:BC.CookieUtil.get('uid'),password:BC.CookieUtil.get('password')},
			s:function(){
				if(document.referrer.indexOf('reg')!=-1){
					window.location.href = BC.getRootPath();
				}else{
					window.location.href = document.referrer;	//返回前一个页面
				}
			},
			f:function(){
				$tips.html("哎呀，记住的用户名跟密码已经过期啦，需要重新登陆哦");
				tipsPic.className="tips-pic pic-info";
				$dialogBox.modal({
					backdrop:false,
					keyboard:false
				});
				$loginContainer.html(oldContent);
				formInit();
				window.setTimeout(function(){$dialogBox.modal('hide')},2000);
			}
		});
	}else{
		formInit();
	}
	function formInit(){
		var $form = $('#login-form'),
			$username = $('.username'),
			$password = $('.password'),
			tipsPic = $('.modal .tips-pic')[0],
			$tips = $('.modal .tips-content'),
			$dialogBox = $('#dialog-box'),
			$loginBtn = $('.login-btn');
			$rememberMe = $('.rememberme');
		$form.submit(function(e){
			e.preventDefault();
			var uid = $.trim($username.val());
			var password = $.trim($password.val());
			if(uid==""){
				$tips.html("别忘了填用户名哦");
				tipsPic.className="tips-pic pic-warn";
				$dialogBox.modal({
					backdrop:false,
					keyboard:false
				});
				$dialogBox.on('hidden.bs.modal',function(){
					$username.focus();
				});
				window.setTimeout(function(){$dialogBox.modal('hide')},1000);
			}else if(password==""){
				$tips.html("别忘了密码呀亲");
				tipsPic.className="tips-pic pic-warn";
				$dialogBox.modal({
					backdrop:false,
					keyboard:false
				});
				$dialogBox.on('hidden.bs.modal',function(){
					$password.focus();
				});
				window.setTimeout(function(){$dialogBox.modal('hide')},1000);
			}else{
				BC.ajax({
					type:'POST',
					url:'/bookcrossing/login',
					dataType:'json',
					data:{username:uid,password:hex_md5(password)},
					b:function(){
						$loginBtn.html('<img src="/bookcrossing/img/loading.gif" />').attr('disabled','disabled');
					},
					s:function(){
						if($rememberMe.prop('checked')){
							BC.CookieUtil.set('uid',uid,1,'/boocrossing/');	
							BC.CookieUtil.set('password',hex_md5(password),1,'/boocrossing/');
						}
						if(document.referrer.indexOf('reg')!=-1){
							window.location.href = BC.getRootPath();
						}else{
							window.location.href = document.referrer;	//返回前一个页面
						}
					},
					f:function(resp){
						$tips.html(resp.msg);
						tipsPic.className="tips-pic pic-error";
						$dialogBox.modal({
							backdrop:false,
							keyboard:false
						});
						window.setTimeout(function(){$dialogBox.modal('hide')},1000);
						$loginBtn.removeAttr('disabled').html('登陆');
					},
					e:function(resp){
						$tips.html('哎呀，服务器出错了...');
						tipsPic.className="tips-pic pic-warn";
						$dialogBox.modal({
							backdrop:false,
							keyboard:false
						});
						window.setTimeout(function(){$dialogBox.modal('hide')},1000);
						$loginBtn.removeAttr('disabled').html('登陆');
					}
				});
			}
		});
	}
})